import json

import pytest

import config
from API.course import CourseAPI
from API.login import LoginAPI
def bulid_data(json_file):
    test_data = []
    with open(json_file,"rb") as f:
        data=json.load(f)
    for i in data:
        name=i["name"]
        subject= i["subject"]
        price=i["price"]
        applicablePerson=i["applicablePerson"]
        info=i["info"]
        message=i["message"]
        status=i["status"]
        js_code=i["js_code"]
        isLogin=i["isLogin"]
        test_data.append((name,subject,price,applicablePerson,info,message,status,js_code,isLogin))
    return test_data
class TestAddCourse:
    uuid=None
    token=None
    def setup_method(self):
        # 实例化
        self.login_api=LoginAPI()
        self.course_api=CourseAPI()
        res_v=self.login_api.get_verify_code()
        TestAddCourse.uuid=res_v.json()["uuid"]
        test_data = {

            "username": "admin",
            "password":"HM_2023_test",
            "code": "2",
            "uuid": TestAddCourse.uuid
        }
        res_login_success = self.login_api.login(test_data=test_data)
        print(res_login_success.json())
        TestAddCourse.token=res_login_success.json()["token"]
    def teardown(self):
        pass
    @pytest.mark.parametrize("name,subject,price,applicablePerson,info,message,status,js_code,isLogin",bulid_data(config.BASIC_PATH+"/data/course_add.json"))
    def test_add_course(self,name,subject,price,applicablePerson,info,message,status,js_code,isLogin):
        add_course_data = {
            "name": name,
            "subject": subject,
            "price": price,
            "applicablePerson": applicablePerson,
            "info": info
        }

        res_add_course = self.course_api.add_course(test_data=add_course_data, token=TestAddCourse.token if isLogin else "")

        assert res_add_course.status_code==200
        assert message in res_add_course.text
        assert js_code==res_add_course.json()["code"]

